import { config } from '../../config.js';
import { Common } from '../../common/common.js';
import { MyModel } from '../../../../modules/my.js';

const my = new MyModel();
const common = new Common();

Page({
  data: {
    hid: null, // 工作经验id
    companyScale: config.data.companyScale, // 公司规模模板
    compType: config.data.compType, // 公司性质
    entryTime: null, // 入职时间
    quitTime: null, // 离职时间
    compName: null, // 公司名称
    posName: null, // 职位名称
    compNature: null, // 公司性质
    compScale: null, // 公司规模
    wordText: null // 工作内容
  },
  bindChange: function(e) {
    let _this = this;
    switch(e.target.id) {
      // 入职时间
      case 'entrytime':
        _this.setData({
          entryTime: e.detail.value
        });
      break;
      // 离职时间
      case 'quittime':
        _this.setData({
          quitTime: e.detail.value
        });
      break;
      // 公司性质
      case 'compnature':
        _this.setData({
          compNature: e.detail.value
        });
      break;
      // 公司规模
      case 'compscale':
        _this.setData({
          compScale: e.detail.value
        });
      break;
      default:
      break;
    }
  },
  // 修改提交
  submit(e) {
    let value = e.detail.value;
    let data = {
      hid: this.data.hid,
      resumeId: common.getRid(),
      entryTime: this.data.entryTime,
      leaveTime: this.data.quitTime,
      workCompanyName: value.compName,
      workPosition: value.posName,
      companyNature: this.data.compNature,
      companyScale: this.data.compScale,
      workContent: value.wordText
    }
    // 判断是否为空
    if(data['entryTime'] == null) {
      wx.showToast({
        title: '入职时间不能为空',
        icon: 'none'
      });
      return false
    }else if(data['leaveTime'] == null) {
      wx.showToast({
        title: '离职时间不能为空',
        icon: 'none'
      });
      return false
    }else if(data['workCompanyName'] == null || data['workCompanyName'].trim() == '') {
      wx.showToast({
        title: '公司名称不能为空',
        icon: 'none'
      });
      return false
    }else if(data['workPosition'] == null || data['workPosition'].trim() == '') {
      wx.showToast({
        title: '职位不能为空',
        icon: 'none'
      });
      return false
    }else if(data['companyNature'] == null) {
      wx.showToast({
        title: '公司性质不能为空',
        icon: 'none'
      });
      return false
    }else if(data['companyScale'] == null) {
      wx.showToast({
        title: '公司规模不能为空',
        icon: 'none'
      });
      return false
    }else if(data['workContent'] == null || data['workContent'].trim() == '') {
      wx.showToast({
        title: '工作内容不能为空',
        icon: 'none'
      });
      return false
    }
    if(data['entryTime'] > data['leaveTime']) {
      wx.showToast({
        title: '入职时间不能大于离职时间!',
        icon: 'none'
      });
      return false
    }
    console.log(data);
    // 提交
    my.editHistory(data)
    .then(res => {
      if(res.code == 2) {
        wx.showToast({
          title: '修改成功',
          icon: 'none',
          duration: 2000
        });
        setTimeout(() => {
          wx.redirectTo({
            url: '../index/index'
          });
        }, 2000)
      }else {
        wx.showToast({
          title: '修改失败，请稍后重试！',
          icon: 'none',
          duration: 2000
        });
      }
    })
  },
  onLoad: function (options) {
    let hid = options.hid;
    if(hid) {
      this.setData({
        hid: hid
      })
    }
    // 回显工作经验
    my.showHistory(hid)
    .then(res => {
      if(res.code == 2) {
        res = res.data.history;
        this.setData({
          entryTime: res.entryTime,
          quitTime: res.leaveTime,
          compName: res.workCompanyName,
          posName: res.workPosition,
          compNature: res.companyNature,
          compScale: res.companyScale,
          wordText: res.workContent,
          loading: true
        })
      }
    })
  }
})